<?php

namespace app\company\model;


use Exception;
use think\Model;

/**
 *  盘点商品
 * @property  int $id
 * @property  int $take_stock_id 盘点单ID
 * @property  int $goods_id 商品ID
 * @property  int $quantity 数量
 * @property  int $is_modify 类型 1 未修改 2 已修改
 * @property  int $origin_quantity 修改前数量(原始数据)
 * @property  int $dif_quantity 修改后差异数量
 * @property  int $goods_rule_id 商品规格id
 * @property float $money 差异金额
 */
class WarehouseTakeStockGoods extends Base
{


    /**
     * @param WarehouseTakeStockGoods $model
     * @return void
     * @throws Exception
     */
    public static function onBeforeUpdate(WarehouseTakeStockGoods $model): void
    {
        if ($model->warehouseTakeStock()->value("stock_status") == 3) exception("盘点单已结束");
        if ($quantity = request()->param('quantity', 0)) {
            $model->dif_quantity = $quantity - $model->origin_quantity;
            if ($model->dif_quantity != 0)$model->is_modify = 2;
            if ($model->dif_quantity == 0)$model->is_modify = 1;
            //得到当前售价
            $money        = CompanyGoodsRule::where('goods_rule_id', $model->goods_rule_id)->withTrashed()->value('money');
            $model->money = $money * $model->dif_quantity;
        }
    }

}